package com.sgcc.electricjournal.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sgcc.electricjournal.dao.JournalMapper;
import com.sgcc.electricjournal.dao.LogContentMapper;
import com.sgcc.electricjournal.entity.PageResult;
import com.sgcc.electricjournal.entity.QueryPageBean;
import com.sgcc.electricjournal.pojo.Journal;
import com.sgcc.electricjournal.pojo.LogContent;
import com.sgcc.electricjournal.pojo.LogDetail;
import com.sgcc.electricjournal.pojo.Login;
import com.sgcc.electricjournal.service.JournalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class
JournalServiceImpl implements JournalService {

    private Logger log = LoggerFactory.getLogger(JournalServiceImpl.class);

    @Autowired
    private JournalMapper journalMapper;
    @Autowired
    private LogContentMapper logContentMapper;

    @Override
    @Transactional
    public void add(Journal journal,String usernameGloble) {

        List<LogContent> contentList = journal.getContentList();

        //如果内容不为空,则先新增内容
        if (!CollectionUtils.isEmpty(contentList)) {

            for (LogContent logContent : contentList
            ) {
                logContent.setId(String.valueOf(UUID.randomUUID()));
                //logContent.setRecorder(usernameGloble);
                //logContent.setCreateTime(new Date());
                logContent.setJournalId(journal.getId());
                int i = logContentMapper.add(logContent);

                if (i != 1) {
                    throw new RuntimeException("新增内容失败");
                }
            }
        }
        //添加Journal

        int j = journalMapper.add(journal);

        if (j != 1) {
            throw new RuntimeException("新增日志失败");
        }

    }

    @Override
    public List<Journal> findAll() {

        List<Journal> journalList = journalMapper.findAll();

        for (Journal journal : journalList
        ) {
            List<LogContent> logContentList = logContentMapper.findLogContentListByJournalId(journal.getId());

            if (!CollectionUtils.isEmpty(logContentList)) {
                journal.setContentList(logContentList);
            }
        }

        return journalList;

    }

    @Override
    public Journal findOneById(String journalId) {

        Journal journal = journalMapper.findOneById(journalId);


        if (journal != null) {
            List<LogContent> logContentList = logContentMapper.findLogContentListByJournalId(journal.getId());

            if (!CollectionUtils.isEmpty(logContentList)) {
                journal.setContentList(logContentList);
            }
        }
        return journal;
    }

    @Override
    @Transactional
    public void deleteOneById(String journalId) {

        journalMapper.deleteOneById(journalId);

        logContentMapper.deleteAllByJournalId(journalId);

    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        queryPageBean.setRelCurrentPage(queryPageBean.getCurrentPage());
        Page<Journal> journalPage = journalMapper.findByCondition(queryPageBean);

        Page<LogDetail> logDetialPage = Journal2LogDetial(journalPage, queryPageBean);

        long total = journalMapper.getTotal(queryPageBean);

        return new PageResult(total, logDetialPage);
    }

    /**
     * 获取导出内容列表
     * @param queryPageBean
     * @return
     */
    @Override
    public List<LogDetail> downExcel(QueryPageBean queryPageBean) {
        List<Journal> list = journalMapper.downExcel(queryPageBean);
        List<LogDetail> logDetails = Journal2LogDetialDownExcel(list);
        return logDetails;
    }

    @Override
    public PageResult findByPageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        queryPageBean.setRelCurrentPage(queryPageBean.getCurrentPage());

        List<String> journalIdList = logContentMapper.findJournalIdList(queryPageBean);


        long total = 0L;
        Page<LogDetail> logDetialPage = new Page<>();
        if(!CollectionUtils.isEmpty(journalIdList)){

            queryPageBean.setJournalIdList(journalIdList);

            Page<Journal> journalPage = journalMapper.findByConditionQuery(queryPageBean);

            logDetialPage = Journal2LogDetial(journalPage, queryPageBean);

            total = journalMapper.getTotalOfQuery(queryPageBean);

            return new PageResult(total, logDetialPage);
        }

/*        Page<Journal> journalPage = journalMapper.findByConditionQuery(queryPageBean);

        Page<LogDetail> logDetialPage = Journal2LogDetial(journalPage, queryPageBean);

        long total = journalMapper.getTotalOfQuery(queryPageBean);*/

        return new PageResult(total, logDetialPage);
    }

    @Override
    public List<LogDetail> downExcelQuery(QueryPageBean queryPageBean) {

        List<String> journalIdList = logContentMapper.findJournalIdList(queryPageBean);
        List<LogDetail> logDetails = new ArrayList<>();
        if(!CollectionUtils.isEmpty(journalIdList)){
            queryPageBean.setJournalIdList(journalIdList);
            List<Journal> list = journalMapper.downExcelQuery(queryPageBean);
            logDetails = Journal2LogDetialDownExcel(list);
        }

        return logDetails;
    }

    /**
     * 用户登录
     * @param login
     * @return
     */
    @Override
    public boolean login(Login login) {

        //查询用户是否存在
        Login loginDB = journalMapper.queryLogin(login);

        if(null == loginDB){
            return false;
        }

        return true;
    }

    /**
     * 使用LogDetail为前端页面展示数据
     * @param journalPage
     * @param queryPageBean
     * @return
     */
    private Page<LogDetail> Journal2LogDetial(Page<Journal> journalPage, QueryPageBean queryPageBean) {

        Page<LogDetail> logDetialPage = new Page<LogDetail>();


        for (int i = 1; i <= journalPage.size(); i++) {

            Journal journal = journalPage.get(i - 1);

            journal.setSerialNum(i+(queryPageBean.getCurrentPage()-1)*queryPageBean.getPageSize());//设置序号

            List<LogContent> contentList = journal.getContentList();



            if (!CollectionUtils.isEmpty(contentList)) {
                for (LogContent logContent : contentList
                ) {
                    LogDetail logDetial = new LogDetail();

                    logDetial.setJournalId(journal.getId());//日志表id
                    logDetial.setSerialNum(journal.getSerialNum());//序号
                    logDetial.setStationName(logContent.getStationName());//厂站
                    //logDetial.setStationName(journal.getStationName());//厂站
                    /*logDetial.setType(journal.getType());//分类:  1异常缺陷   2计划检修  3临时检修
                    logDetial.setStatus(journal.getStatus());//状态: 0删除  1未完成   2已完成*/
                    logDetial.setType(tranType(logContent.getType()));//分类:  1异常缺陷   2计划检修  3临时检修
                    logDetial.setStatus(tranStatus(journal.getStatus()));//状态: 0删除  1未完成   2已完成
                    logDetial.setJournalCreateTime(journal.getCreateTime());//日志创建时间
                    logDetial.setJournalUpdateTime(journal.getUpdateTime());//日志更新时间

                    logDetial.setLogContentId(logContent.getId());//日志内容表id
                    logDetial.setContent(logContent.getContent());//内容
                    logDetial.setRecorder(logContent.getRecorder());//记录人
                    logDetial.setLogContentCreateTime(logContent.getCreateTime());//内容创建时间
                    logDetial.setLogContentUpdateTime(logContent.getUpdateTime());//内容更新时间

                    logDetialPage.add(logDetial);
                }
            }
        }
        return logDetialPage;
    }
    /**
     * 使用LogDetail为前端页面展示数据
     * @param journalList
     * @return
     */
    private List<LogDetail> Journal2LogDetialDownExcel(List<Journal> journalList) {

        List<LogDetail> logDetialList = new ArrayList<>();


        for (int i = 1; i <= journalList.size(); i++) {

            Journal journal = journalList.get(i - 1);

            journal.setSerialNum(i);//设置序号

            List<LogContent> contentList = journal.getContentList();

            if (!CollectionUtils.isEmpty(contentList)) {
                for (LogContent logContent : contentList
                ) {
                    LogDetail logDetial = new LogDetail();

                    logDetial.setJournalId(journal.getId());//日志表id
                    logDetial.setSerialNum(journal.getSerialNum());//序号
                    logDetial.setStationName(logContent.getStationName());//厂站

                    logDetial.setType(tranType(logContent.getType()));//分类:  1异常缺陷   2计划检修  3临时检修
                    logDetial.setStatus(tranStatus(journal.getStatus()));//状态: 0删除  1未完成   2已完成
                    logDetial.setJournalCreateTime(journal.getCreateTime());//日志创建时间
                    logDetial.setJournalUpdateTime(journal.getUpdateTime());//日志更新时间

                    logDetial.setLogContentId(logContent.getId());//日志内容表id
                    logDetial.setContent(logContent.getContent());//内容
                    logDetial.setRecorder(logContent.getRecorder());//记录人
                    logDetial.setLogContentCreateTime(logContent.getCreateTime());//内容创建时间
                    logDetial.setLogContentUpdateTime(logContent.getUpdateTime());//内容更新时间

                    logDetialList.add(logDetial);
                }
            }
        }
        return logDetialList;
    }

    /**
     * 类型转换
     * @param num
     * @return
     */
    public  String tranType(String num){
        switch (num)
        {
            case "0":
                return "异常缺陷";
            case "1":
                return "计划检修";
            case "2":
                return "临时检修";
            case "3":
                return "调到命令";
            case "4":
                return "非计划检修";
            case "5":
                return "故障跳闸";
            case "6":
                return "省调通知";
            case "7":
                return "领导指示";
            case "8":
                return "机组启停";
            case "9":
                return "其他";
            default:
                return "";
        }
    }


    /**
     * 状态转换
     * @param num
     * @return
     */
    public  String tranStatus(String num){
        switch (num)
        {
            case "0":
                return "删除";
            case "1":
                return "未完成";
            case "2":
                return "已完成";
            default:
                return "";
        }
    }
}
